package ATBMS.modles.DAO;

import ATBMS.modles.Ticket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.ArrayList;
import java.util.List;

/**
 * Ticket DAO Implement
 */
public class TicketDAOImpl implements TicketDAO {

    private String errorMessage;
    private Connection conn;

    public void updateUsersTelphone(int uid, String newTelphoneNumber) {
        try {
            PreparedStatement ps = conn.prepareStatement("UPDATE atbms_ticket " +
                    "SET utel=? WHERE uid=?");
            ps.setString(1, newTelphoneNumber);
            ps.setInt(2, uid);
            ps.execute();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private TicketDAOImpl(Connection conn) {
        this.conn = conn;
    }

    public static TicketDAO getInstance(Connection conn) {
        return new TicketDAOImpl(conn);
    }

    public void delete(int tid) {
        try {
            PreparedStatement ps = conn.prepareStatement("DELETE FROM atbms_ticket WHERE tid=?");
            ps.setInt(1, tid);
            ps.execute();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Ticket> findByUserID(int uid) {
        List<Ticket> ret = new ArrayList<Ticket>();
        try {
            PreparedStatement ps = conn.prepareStatement("SELECT tid,fcode," +
                    "uid,booktime,utel FROM atbms_ticket WHERE uid=?");
            ps.setInt(1, uid);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                ret.add(new Ticket(rs.getInt(1), rs.getString(2),
                        rs.getInt(3), rs.getTimestamp(4), rs.getString(5)));
            }
            rs.close();
            ps.close();
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Ticket> findByTel(String tel) {
        List<Ticket> ret = new ArrayList<Ticket>();
        try {
            PreparedStatement ps = conn.prepareStatement("SELECT tid,fcode," +
                    "uid,booktime,utel FROM atbms_ticket WHERE uid=?");
            ps.setString(1, tel);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                ret.add(new Ticket(rs.getInt(1), rs.getString(2),
                        rs.getInt(3), rs.getTimestamp(4), rs.getString(5)));
            }
            rs.close();
            ps.close();
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean insert(Ticket ticket) {
        try {
            PreparedStatement ps = conn.prepareStatement("INSERT INTO atbms_ticket SET " +
                    "fcode=?," +
                    "uid=?," +
                    "booktime=?," +
                    "utel=?");

            ps.setString(1, ticket.getFcode());
            ps.setInt(2, ticket.getUid());
            ps.setTimestamp(3, new java.sql.Timestamp(ticket.getBooktime().getTime()));
            ps.setString(4, ticket.getUtel());

            ps.execute();
            SQLWarning sqlError = ps.getWarnings();
            boolean ret = (sqlError == null);
            setErrorMessage(sqlError);
            ps.close();
            return ret;
        } catch (SQLException e) {
            setErrorMessage(e);
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void update(int tid, Ticket ticket) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    private void setErrorMessage(SQLException err) {
        if (err == null) {
            return;
        }

        errorMessage = err.getMessage();
    }

    private void setErrorMessage(SQLWarning warning) {
        if (warning == null) {
            return;
        }

        errorMessage = warning.getMessage();
    }
}
